package cn.com.jsepc.rep.report;

import cn.com.jsepc.rep.comm.Common;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.com.jsepc.rep.comm.ConstUtil;
import cn.com.jsepc.rep.vo.ReportParaVO;

import com.jarp.db.DAO;

public class reportGrid {

    //查询数据
    public String getReportGrid(String repid) {
        String res = "";
        DAO dao = new DAO();
        try {
            List<ReportParaVO> lstResults = new ArrayList<ReportParaVO>();
            String sql = "select * from TB_REPORT_PARA where repid=" + repid;
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
            if (v != null) {
                for (int i = 0; i < v.size(); i++) {
                    Hashtable hashRows = (Hashtable) v.get(i);
                    ReportParaVO reportVO = new ReportParaVO();
                    reportVO.setParaid((String) hashRows.get("PARAID"));
                    reportVO.setRepid((String) hashRows.get("REPID"));
                    reportVO.setParaname((String) hashRows.get("PARANAME"));
                    reportVO.setSourcedep((String) hashRows.get("SOURCEDEP"));
                    reportVO.setContactor((String) hashRows.get("CONTACTOR"));
                    reportVO.setParaunit((String) hashRows.get("PARAUNIT"));
                    reportVO.setUpdatatime((String) hashRows.get("UPDATATIME"));
                    reportVO.setIssaved((String) hashRows.get("ISSAVED"));
                    lstResults.add(reportVO);
                }
                JSONArray json = new JSONArray();
                json = JSONArray.fromObject(lstResults);
                res = json.toString();
            }else{
            	return "[]";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("NewProjectTree getXmDl Exception.");
        }
        return res;
    }
    
   

    public synchronized String checkRepid(String repid) throws ServletException {
    	String res="";
        try {
            DAO dao = new DAO();
            String sqll = "select * from TB_REPORT_PARA where repid =" + repid ;
            Vector v1 = dao.getDataSet(ConstUtil.DBNAME, sqll);
            System.out.println(v1);
            if (v1 != null) {
            	res="false";
            } else {
            	res="true";
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return res;
    }
    
    
    

    //判断调用的方法
    public synchronized String checkId(String itemJsonInfo) throws ServletException {
        try {
            JSONObject jsonObject = new JSONObject(itemJsonInfo);
            DAO dao = new DAO();
            reportGrid ds = new reportGrid();
            String id = jsonObject.getString("paraid");
            String sqll = "select * from TB_REPORT_PARA where paraid =" + id + "";
            Vector v1 = dao.getDataSet(ConstUtil.DBNAME, sqll);
            System.out.println(v1);
            if (v1 != null) {
                ds.ReportUpdate(itemJsonInfo);
            } else {
                ds.ReportAdd(itemJsonInfo);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return "ok";
    }

    //添加
    public synchronized String ReportAdd(String itemJsonInfo) throws ServletException {
        String res="";
        try {
            JSONObject jsonObject = new JSONObject(itemJsonInfo);
            DAO dao = new DAO();
            ReportParaVO reportVO = new ReportParaVO();
            reportVO.setParaid(jsonObject.getString("paraid"));
            reportVO.setRepid(jsonObject.getString("repid"));
            reportVO.setParaname(jsonObject.getString("paraname"));
            reportVO.setSourcedep(jsonObject.getString("sourcedep"));
            reportVO.setContactor(jsonObject.getString("contactor"));
            reportVO.setParaunit(jsonObject.getString("paraunit"));
            reportVO.setUpdatatime(jsonObject.getString("updatatime"));
            reportVO.setIssaved(jsonObject.getString("issaved"));
            
            String paraid=new Common().getSeqStr("seq_report_para");
           // System.out.println(paraid);
            String sql = "insert into  TB_REPORT_PARA("
                    + "paraid ,"
                    + "repid,"
                    + "paraname,"
                    + "sourcedep,"
                    + "contactor,"
                    + "paraunit,"
                    + "updatatime,"
                    + "issaved) values(" + paraid + "," + reportVO.getRepid() + ",'"
                    + reportVO.getParaname() + "','" + reportVO.getSourcedep() + "','" + reportVO.getContactor()
                    + "','" + reportVO.getParaunit() + "'," + reportVO.getUpdatatime() + "," + reportVO.getIssaved() + ")";
           // System.out.println(sql);

            int bRet = dao.execCommand("Jarp", sql);
            if (bRet == 1) {
                System.out.println("添加成功");
               res="true";
            } else {
                System.out.println("添加失败");
               // return "false";
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return  res;
    }

    //修改
    public synchronized String ReportUpdate(String itemJsonInfo) throws ServletException {
        String res="";
        try {
            JSONObject jsonObject = new JSONObject(itemJsonInfo);
            DAO dao = new DAO();
            ReportParaVO reportVO = new ReportParaVO();
            reportVO.setParaid(jsonObject.getString("paraid"));
            reportVO.setRepid(jsonObject.getString("repid"));
            reportVO.setParaname(jsonObject.getString("paraname"));
            reportVO.setSourcedep(jsonObject.getString("sourcedep"));
            reportVO.setContactor(jsonObject.getString("contactor"));
            reportVO.setParaunit(jsonObject.getString("paraunit"));
            reportVO.setUpdatatime(jsonObject.getString("updatatime"));
            reportVO.setIssaved(jsonObject.getString("issaved"));

            String paraid = reportVO.getParaid();
            String repid = reportVO.getRepid();
            String paraname = reportVO.getParaname();
            String sourcedep = reportVO.getSourcedep();
            String contactor = reportVO.getContactor();
            String paraunit = reportVO.getParaunit();
            String updatatime = reportVO.getUpdatatime();
            String issaved = reportVO.getIssaved();

            String sql = "update TB_REPORT_PARA set repid=" + repid + ",paraname='"
                    + paraname + "',sourcedep='" + sourcedep + "',contactor='" + contactor
                    + "',paraunit='" + paraunit + "',updatatime=" + updatatime + ",issaved="
                    + issaved + " where paraid=" + paraid;

            int bRet = dao.execCommand("Jarp", sql);
            if (bRet == 1) {
                System.out.println("修改成功");
                res="true";
            } else {
                System.out.println("修改失败");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
       return  res;
    }

    //删除
    public synchronized String ReportDelete(String paraid) throws ServletException {
        String re="";
        DAO dao = new DAO();
        try {
            String sql = "delete from TB_REPORT_PARA where paraid =" + paraid  ;
            int res = dao.execCommand("Jarp", sql);
            if (res == 1) {
                System.out.println("删除成功");
                re="true";
            } else {
                System.out.println("删除失败");
                re="false";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("NewProjectTree getXmDl Exception.");
        }
          return re;
    }   
    
}
